<?php


namespace app\admin\controller;


use app\admin\model\User;
use app\common\controller\admin\Base;

class Ipgroup extends Base
{
    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\IpGroup();
    }


    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $page = $this->request->post('page', 1);
//            $size = $this->request->post('page_size', 30);
            $size = 30;
            $status = $this->request->post('status', '');

            if ($status != '') {
                $where['status'] = $status;
            }
            $where['is_del'] = 0;
            $list = $this->model
                ->field('*')
                ->where($where)
                ->page($page, $size)
                ->order('id', 'desc')
                ->select()
                ->toArray();
            //查看组下有多少ip
            $sysForbiddenIp = new \app\admin\model\SysForbiddenIp();
            foreach ($list as $key => $value) {
                $value['ip_num'] = $sysForbiddenIp->where(['ip_group_id' => $value['id']])->count('id');
                $list[$key] = $value;
            }
            $total = $this->model->where($where)->count('id');
            $totalPage = ceil($total / $size);
            $data = [
                'total' => $total,
                'list' => $list,
                'totalPage' => $totalPage
            ];
            $this->success('列表', '', $data);
        }
    }

    /**
     * 数据的设置操作, 目前有  开关  是否默认  状态
     */
    public function setup($ids = "")
    {
        $type = $this->request->post("type");
        $val = $this->request->post("val");
        $field = $type;
        switch ($type) {
            case 'status':
                $data[$field] = $val;
                $where['id'] = ['in', $ids];
                break;
            default:
                $this->error('不存在的业务');
                break;
        }
        $this->model->update($data, $where);
        $this->success('操作成功');
    }
}